//
//  ANBaseTableViewController.m
//  Ants
//
//  Created by ZHM on 16/3/17.
//  Copyright © 2016年 ZHM. All rights reserved.
//

#import "ANBaseTableViewController.h"

#import "ANGuoupItem.h"
#import "ANProfileBaseItem.h"
#import "ANTableViewProfileCell.h"

@interface ANBaseTableViewController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation ANBaseTableViewController

- (NSMutableArray *)guoups {
    if (!_guoups) {
        _guoups = [NSMutableArray array];
    }
    return _guoups;
}

//分组样式
//- (instancetype)init {
//    return [self initWithStyle:UITableViewStyleGrouped];
//}

//返回头部标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    ANGuoupItem *group = self.guoups[section];
    return group.headedTitle;
}
//返回尾部标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
    ANGuoupItem *group = self.guoups[section];
    return group.footerTitle;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _tableView = [[UITableView alloc] initWithFrame:ANKeyWindow.bounds style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
}

- (NSString *)pageName {
    NSString *name = NSStringFromClass([self class]);
    name = [name stringByReplacingOccurrencesOfString:@"AN" withString:@""];
    name = [name stringByReplacingOccurrencesOfString:@"Controller" withString:@""];
    name = [name stringByReplacingOccurrencesOfString:@"View" withString:@""];
    //NSLog(@"name = %@",name);
    return name;
}
/**
 *  统计界面跳转情况
 *  调用 beginLogPageView 后，在 endLogPageView 调用之前，不要再发生调用 beginLogPageView 的情况
 *  如果出现上述情况，请检查是否有 embedded ViewController
 *  对于 UINavigationViewController 和 UITabbarViewController 等，要统计他们的 Child ViewController，而不是统计他们自身
 */
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
//    [MLAnalytics beginLogPageView:[self pageName]];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    
//    [MLAnalytics endLogPageView:[self pageName]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.guoups.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    ANGuoupItem *guoupItem = self.guoups[section];
    return guoupItem.items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ANTableViewProfileCell *cell = [ANTableViewProfileCell cellWithTableView:tableView];
    ANGuoupItem *group = self.guoups[indexPath.section];
    ANProfileBaseItem *item = group.items[indexPath.row];
    cell.item = item;
    return cell;
}
#pragma mark -点击CELL调用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.indexCellClick = indexPath;
    ANGuoupItem *group = self.guoups[indexPath.section];
    ANProfileBaseItem *item = group.items[indexPath.row];
    if (item.option) {
        item.option(item);
    }else if (item.destVcClass) {
        UIViewController *vc = [[item.destVcClass alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
}


@end
